package com.navcoo.todolist.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.navcoo.todolist.mapper.TodoDataMapper;
import com.navcoo.todolist.pojo.dao.TodoDataDAO;
import com.navcoo.todolist.service.DeleteTodoData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 删除待办数据
 *
 * @author Navcoo
 * @date 2024-02-12
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeleteTodoDataImpl implements DeleteTodoData {

    /**
     * Todo数据映射器
     */
    private final TodoDataMapper todoDataMapper;


    /**
     * 删除待办事项数据（逻辑删除）
     *
     * @param instId 数据ID
     * @return {@link JSONObject}
     * @throws Exception 异常
     */
    @Override
    public JSONObject deleteTodoData(String instId) throws Exception {

        // 查询数据
        QueryWrapper<TodoDataDAO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TodoDataDAO::getIsDelete, false);
        wrapper.lambda().eq(TodoDataDAO::getInstId, instId);

        // 创建更新数据
        TodoDataDAO todoDataDAO = new TodoDataDAO();
        todoDataDAO.setIsDelete(true);
        todoDataDAO.setUpdatedAt(LocalDateTime.now());

        // 更新数据
        int update = todoDataMapper.update(todoDataDAO, wrapper);

        JSONObject result = new JSONObject();

        if (update <= 0) {
            log.info("删除数据失败");
        } else {
            // 成功
            log.info("删除数据成功");
            result.set("instId", instId);
        }

        return result;
    }

}
